<?php
//留言

namespace app\admin\controller;

use \think\Db;
use app\admin\controller\Permissions;
class LeaveMessage extends Permissions
{
    /**
     * 招聘信息列表
     */
    public function index()
    {

        $state=$this->request->Post('state');
        $type=$this->request->Post('type');
        $create_time=strtotime($this->request->Post('create_time'));
        $end_time=strtotime($this->request->Post('end_time'));
        if($state !='') $where['l.state']= ['=',$state];
        if($type !='') $where['l.type']= ['=',$type];
        if($create_time !='') $where['l.create_time']= ['>',$create_time];
        if($end_time !='') $where['l.create_time']= ['<',$end_time];
            $join=[
            ["users u","u.id=l.user_id","LEFT"],
        ];

        $message = empty($where)?Db::name('leave_message')->alias('l')->join($join)->field("l.*,u.name")->order("l.id","desc")->paginate(10): Db::name('leave_message')->alias('l')->join($join)->field("l.*,u.name")->where($where)->order("id","desc")->paginate(10,true,['query'=>request()->Post()]);
        $this->assign('message',$message);
        // var_dump($news);
        return $this->fetch();
    }

    /**
     * 修改留言
     */
    public function add(){

        //判断是不是操作数据库  
        if($this->request->isPost()){

            $post=$this->request->Post();
            $id=$this->request->Post("id");
            $data=["content"=>$post['content'],"state"=>$post['state']];
            $res = Db::name("leave_message")->where("id",$id)->update($data);
            if($res==false) return $this->error('修改失败');     
            return $this->success("信息保存成功",'admin/leave_message/index');
        }else{
           //获取对应数据id
            $id=$this->request->param("id");
            $leave_message=Db::name("leave_message")->where("id",$id)->find();
            // var_dump($employ);
            $this->assign("leave_message",$leave_message);
            return $this->fetch();
        }
    }


     /**
     * 留言删除
     * @return [type] [description]
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('leave_message')->where('id',$id)->delete()) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    /**
     * 回复留言
     */
    public function reply(){

        //判断是不是操作数据库  
        if($this->request->isPost()){

            $post=$this->request->Post();
            $m_id=$post['m_id'];
            $data=[
                'content'=>$post['content'],
                'm_id'=>$post['id'],
                'reply_time'=>time(),
            ];
            if($m_id==''){
                $res = Db::name("message_reply")->insert($data);
            }else{
                $res = Db::name("message_reply")->where("m_id",$m_id)->update(['content'=>$post['content'],"reply_time"=>time()]);
            }
            
            if($res==false){
                return $this->error('回复失败');  
            }else{
                Db::name("leave_message")->where('id',$post['id'])->update(['type'=>1]);
            }
            return $this->success("留言回复成功",'admin/leave_message/index');
        }else{
           //获取对应数据id
            $id=$this->request->param("id");
            $leave_message=Db::name("leave_message")->where("id",$id)->find();
            $reply=Db::name("message_reply")->where("m_id",$id)->find();
            // var_dump($employ);
            $this->assign("leave_message",$leave_message);
            $this->assign("reply",$reply);
            return $this->fetch();
        }
    }

    
}
